home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gdevpdfb.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  18.6 KB  |  599 lines

  1. /* Copyright (C) 2000 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gdevpdfb.c,v 1.2.2.1 2000/11/09 23:24:18 rayjj Exp $ */
  20. /* Low-level bitmap image handling for PDF-writing driver */
  21. #include "string_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gdevpdfx.h"
  25. #include "gdevpdff.h"        /* for synthesized bitmap fonts */
  26. #include "gdevpdfg.h"
  27. #include "gdevpdfo.h"        /* for data stream */
  28. #include "gxcspace.h"
  29.  
  30. /* We need this color space type for constructing temporary color spaces. */
  31. extern const gs_color_space_type gs_color_space_type_Indexed;
  32.  
  33. /* ---------------- Utilities ---------------- */
  34.  
  35. /* Fill in the image parameters for a bitmap image. */
  36. private void
  37. pdf_make_bitmap_image(gs_image_t * pim, int x, int y, int w, int h)
  38. {
  39.     pim->Width = w;
  40.     pim->Height = h;
  41.     pdf_make_bitmap_matrix(&pim->ImageMatrix, x, y, w, h, h);
  42. }
  43.  
  44. /* ---------------- Driver procedures ---------------- */
  45.  
  46. /* Copy a mask bitmap.  for_pattern = -1 means put the image in-line, */
  47. /* 1 means put the image in a resource. */
  48. private int
  49. pdf_copy_mask_data(gx_device_pdf * pdev, const byte * base, int sourcex,
  50.            int raster, gx_bitmap_id id, int x, int y, int w, int h,
  51.            gs_image_t *pim, pdf_image_writer *piw,
  52.            int for_pattern)
  53. {
  54.     ulong nbytes;
  55.     int code;
  56.     const byte *row_base;
  57.     int row_step;
  58.     long pos;
  59.     bool in_line;
  60.  
  61.     gs_image_t_init_mask(pim, true);
  62.     pdf_make_bitmap_image(pim, x, y, w, h);
  63.     nbytes = ((ulong)w * h + 7) / 8;
  64.  
  65.     if (for_pattern) {
  66.     /*
  67.      * Patterns must be emitted in order of increasing user Y, i.e.,
  68.      * the opposite of PDF's standard image order.
  69.      */
  70.     row_base = base + (h - 1) * raster;
  71.     row_step = -raster;
  72.     in_line = for_pattern < 0;
  73.     } else {
  74.     row_base = base;
  75.     row_step = raster;
  76.     in_line = nbytes <= MAX_INLINE_IMAGE_BYTES;
  77.     pdf_put_image_matrix(pdev, &pim->ImageMatrix, 1.0);
  78.     /*
  79.      * Check whether we've already made an XObject resource for this
  80.      * image.
  81.      */
  82.     if (id != gx_no_bitmap_id) {
  83.         piw->pres = pdf_find_resource_by_gs_id(pdev, resourceXObject, id);
  84.         if (piw->pres)
  85.         return 0;
  86.     }
  87.     }
  88.     /*
  89.      * We have to be able to control whether to put Pattern images in line,
  90.      * to avoid trying to create an XObject resource while we're in the
  91.      * middle of writing a Pattern resource.
  92.      */
  93.     if (for_pattern < 0)
  94.     pputs(pdev->strm, "q ");
  95.     if ((code = pdf_begin_write_image(pdev, piw, id, w, h, NULL, in_line)) < 0 ||
  96.     (code = psdf_setup_lossless_filters((gx_device_psdf *) pdev,
  97.                         &piw->binary,
  98.                         (gs_pixel_image_t *)pim)) < 0 ||
  99.     (code = pdf_begin_image_data(pdev, piw, (const gs_pixel_image_t *)pim,
  100.                      NULL)) < 0
  101.     )
  102.     return code;
  103.     pos = stell(pdev->streams.strm);
  104.     pdf_copy_mask_bits(piw->binary.strm, row_base, sourcex, row_step, w, h, 0);
  105.     cos_stream_add_since(piw->data, pos);
  106.     pdf_end_image_binary(pdev, piw, piw->height);
  107.     return pdf_end_write_image(pdev, piw);
  108. }
  109.  
  110. /* Copy a monochrome bitmap or mask. */
  111. private int
  112. pdf_copy_mono(gx_device_pdf *pdev,
  113.           const byte *base, int sourcex, int raster, gx_bitmap_id id,
  114.           int x, int y, int w, int h, gx_color_index zero,
  115.           gx_color_index one, const gx_clip_path *pcpath)
  116. {
  117.     int code;
  118.     gs_color_space cs;
  119.     cos_value_t cs_value;
  120.     cos_value_t *pcsvalue;
  121.     byte palette[sizeof(gx_color_index) * 2];
  122.     gs_image_t image;
  123.     pdf_image_writer writer;
  124.     pdf_stream_position_t ipos;
  125.     pdf_resource_t *pres = 0;
  126.     byte invert = 0;
  127.     bool in_line = false;
  128.     long pos;
  129.  
  130.     /* Update clipping. */
  131.     if (pdf_must_put_clip_path(pdev, pcpath)) {
  132.     code = pdf_open_page(pdev, PDF_IN_STREAM);
  133.     if (code < 0)
  134.         return code;
  135.     pdf_put_clip_path(pdev, pcpath);
  136.     }
  137.     /* We have 3 cases: mask, inverse mask, and solid. */
  138.     if (zero == gx_no_color_index) {
  139.     if (one == gx_no_color_index)
  140.         return 0;
  141.     /* If a mask has an id, assume it's a character. */
  142.     if (id != gx_no_bitmap_id && sourcex == 0) {
  143.         pdf_set_pure_color(pdev, one, &pdev->fill_color,
  144.                    &psdf_set_fill_color_commands);
  145.         pres = pdf_find_resource_by_gs_id(pdev, resourceCharProc, id);
  146.         if (pres == 0) {    /* Define the character in an embedded font. */
  147.         pdf_char_proc_t *pcp;
  148.         int y_offset;
  149.         int max_y_offset =
  150.         (pdev->open_font == 0 ? 0 :
  151.          pdev->open_font->max_y_offset);
  152.  
  153.         gs_image_t_init_mask(&image, false);
  154.         invert = 0xff;
  155.         pdf_make_bitmap_image(&image, x, y, w, h);
  156.         y_offset =
  157.             image.ImageMatrix.ty - (int)(pdev->text.current.y + 0.5);
  158.         if (x < pdev->text.current.x ||
  159.             y_offset < -max_y_offset || y_offset > max_y_offset
  160.             )
  161.             y_offset = 0;
  162.         /*
  163.          * The Y axis of the text matrix is inverted,
  164.          * so we need to negate the Y offset appropriately.
  165.          */
  166.         code = pdf_begin_char_proc(pdev, w, h, 0, y_offset, id,
  167.                        &pcp, &ipos);
  168.         if (code < 0)
  169.             return code;
  170.         y_offset = -y_offset;
  171.         pprintd3(pdev->strm, "0 0 0 %d %d %d d1\n", y_offset,
  172.              w, h + y_offset);
  173.         pprintd3(pdev->strm, "%d 0 0 %d 0 %d cm\n", w, h,
  174.              y_offset);
  175.         code = pdf_begin_write_image(pdev, &writer, gs_no_id, w, h, NULL, true);
  176.         if (code < 0)
  177.             return code;
  178.         pcp->rid = id;
  179.         pres = (pdf_resource_t *) pcp;
  180.         goto wr;
  181.         }
  182.         pdf_make_bitmap_matrix(&image.ImageMatrix, x, y, w, h, h);
  183.         goto rx;
  184.     }
  185.     pdf_set_pure_color(pdev, one, &pdev->fill_color,
  186.                &psdf_set_fill_color_commands);
  187.     gs_image_t_init_mask(&image, false);
  188.     invert = 0xff;
  189.     } else if (one == gx_no_color_index) {
  190.     gs_image_t_init_mask(&image, false);
  191.     pdf_set_pure_color(pdev, zero, &pdev->fill_color,
  192.                &psdf_set_fill_color_commands);
  193.     } else if (zero == pdev->black && one == pdev->white) {
  194.     gs_cspace_init_DeviceGray(&cs);
  195.     gs_image_t_init(&image, &cs);
  196.     } else if (zero == pdev->white && one == pdev->black) {
  197.     gs_cspace_init_DeviceGray(&cs);
  198.     gs_image_t_init(&image, &cs);
  199.     invert = 0xff;
  200.     } else {
  201.     /*
  202.      * We think this code is never executed when interpreting PostScript
  203.      * or PDF: the library never uses monobit non-mask images
  204.      * internally, and high-level images don't go through this code.
  205.      * However, we still want the code to work.
  206.      */
  207.     gs_color_space cs_base;
  208.     gx_color_index c[2];
  209.     int i, j;
  210.     int ncomp = pdev->color_info.num_components;
  211.     byte *p;
  212.  
  213.     code = pdf_cspace_init_Device(&cs_base, ncomp);
  214.     if (code < 0)
  215.         return code;
  216.     c[0] = psdf_adjust_color_index((gx_device_vector *)pdev, zero);
  217.     c[1] = psdf_adjust_color_index((gx_device_vector *)pdev, one);
  218.     gs_cspace_init(&cs, &gs_color_space_type_Indexed, NULL);
  219.     cs.params.indexed.base_space = *(gs_direct_color_space *)&cs_base;
  220.     cs.params.indexed.hival = 1;
  221.     p = palette;
  222.     for (i = 0; i < 2; ++i)
  223.         for (j = ncomp - 1; j >= 0; --j)
  224.         *p++ = (byte)(c[i] >> (j * 8));
  225.     cs.params.indexed.lookup.table.data = palette;
  226.     cs.params.indexed.lookup.table.size = p - palette;
  227.     cs.params.indexed.use_proc = false;
  228.     gs_image_t_init(&image, &cs);
  229.     image.BitsPerComponent = 1;
  230.     }
  231.     pdf_make_bitmap_image(&image, x, y, w, h);
  232.     {
  233.     ulong nbytes = (ulong) ((w + 7) >> 3) * h;
  234.  
  235.     in_line = nbytes <= MAX_INLINE_IMAGE_BYTES;
  236.     if (in_line)
  237.         pdf_put_image_matrix(pdev, &image.ImageMatrix, 1.0);
  238.     code = pdf_open_page(pdev, PDF_IN_STREAM);
  239.     if (code < 0)
  240.         return code;
  241.     code = pdf_begin_write_image(pdev, &writer, gs_no_id, w, h, NULL, in_line);
  242.     if (code < 0)
  243.         return code;
  244.     }
  245.   wr:
  246.     if (image.ImageMask)
  247.     pcsvalue = NULL;
  248.     else {
  249.     code = pdf_color_space(pdev, &cs_value, &cs,
  250.                    &writer.pin->color_spaces, in_line);
  251.     if (code < 0)
  252.         return code;
  253.     pcsvalue = &cs_value;
  254.     }
  255.     /*
  256.      * There are 3 different cases at this point:
  257.      *      - Writing an in-line image (pres == 0, writer.pres == 0);
  258.      *      - Writing an XObject image (pres == 0, writer.pres != 0);
  259.      *      - Writing the image for a CharProc (pres != 0).
  260.      * We handle them with in-line code followed by a switch,
  261.      * rather than making the shared code into a procedure,
  262.      * simply because there would be an awful lot of parameters
  263.      * that would need to be passed.
  264.      */
  265.     if (pres) {
  266.     /* Always use CCITTFax 2-D for character bitmaps. */
  267.     psdf_CFE_binary(&writer.binary, image.Width, image.Height, false);
  268.     } else {
  269.     /* Use the Distiller compression parameters. */
  270.     psdf_setup_image_filters((gx_device_psdf *) pdev, &writer.binary,
  271.                  (gs_pixel_image_t *)&image, NULL, NULL);
  272.     }
  273.     pdf_begin_image_data(pdev, &writer, (const gs_pixel_image_t *)&image,
  274.              pcsvalue);
  275.     pos = stell(pdev->streams.strm);
  276.     code = pdf_copy_mask_bits(writer.binary.strm, base, sourcex, raster,
  277.                   w, h, invert);
  278.     if (code < 0)
  279.     return code;
  280.     code = cos_stream_add_since(writer.data, pos);
  281.     pdf_end_image_binary(pdev, &writer, writer.height);
  282.     if (!pres) {
  283.     switch ((code = pdf_end_write_image(pdev, &writer))) {
  284.         default:        /* error */
  285.         return code;
  286.         case 1:
  287.         return 0;
  288.         case 0:
  289.         return pdf_do_image(pdev, writer.pres, &image.ImageMatrix,
  290.                     true);
  291.     }
  292.     }
  293.     writer.end_string = "";    /* no Q */
  294.     switch ((code = pdf_end_write_image(pdev, &writer))) {
  295.     default:        /* error */
  296.     return code;
  297.     case 0:            /* not possible */
  298.     return_error(gs_error_Fatal);
  299.     case 1:
  300.     break;
  301.     }
  302.     code = pdf_end_char_proc(pdev, &ipos);
  303.     if (code < 0)
  304.     return code;
  305.   rx:{
  306.     gs_matrix imat;
  307.  
  308.     imat = image.ImageMatrix;
  309.     imat.xx /= w;
  310.     imat.xy /= h;
  311.     imat.yx /= w;
  312.     imat.yy /= h;
  313.     return pdf_do_char_image(pdev, (const pdf_char_proc_t *)pres, &imat);
  314.     }
  315. }
  316. int
  317. gdev_pdf_copy_mono(gx_device * dev,
  318.            const byte * base, int sourcex, int raster, gx_bitmap_id id,
  319.            int x, int y, int w, int h, gx_color_index zero,
  320.            gx_color_index one)
  321. {
  322.     gx_device_pdf *pdev = (gx_device_pdf *) dev;
  323.  
  324.     if (w <= 0 || h <= 0)
  325.     return 0;
  326.     return pdf_copy_mono(pdev, base, sourcex, raster, id, x, y, w, h,
  327.              zero, one, NULL);
  328. }
  329.  
  330. /* Copy a color bitmap.  for_pattern = -1 means put the image in-line, */
  331. /* 1 means put the image in a resource. */
  332. private int
  333. pdf_copy_color_data(gx_device_pdf * pdev, const byte * base, int sourcex,
  334.             int raster, gx_bitmap_id id, int x, int y, int w, int h,
  335.             gs_image_t *pim, pdf_image_writer *piw,
  336.             int for_pattern)
  337. {
  338.     int depth = pdev->color_info.depth;
  339.     int bytes_per_pixel = depth >> 3;
  340.     gs_color_space cs;
  341.     cos_value_t cs_value;
  342.     ulong nbytes;
  343.     int code = pdf_cspace_init_Device(&cs, bytes_per_pixel);
  344.     const byte *row_base;
  345.     int row_step;
  346.     long pos;
  347.     bool in_line;
  348.  
  349.     if (code < 0)
  350.     return code;        /* can't happen */
  351.     gs_image_t_init(pim, &cs);
  352.     pdf_make_bitmap_image(pim, x, y, w, h);
  353.     pim->BitsPerComponent = 8;
  354.     nbytes = (ulong)w * bytes_per_pixel * h;
  355.  
  356.     if (for_pattern) {
  357.     /*
  358.      * Patterns must be emitted in order of increasing user Y, i.e.,
  359.      * the opposite of PDF's standard image order.
  360.      */
  361.     row_base = base + (h - 1) * raster;
  362.     row_step = -raster;
  363.     in_line = for_pattern < 0;
  364.     } else {
  365.     row_base = base;
  366.     row_step = raster;
  367.     in_line = nbytes <= MAX_INLINE_IMAGE_BYTES;
  368.     pdf_put_image_matrix(pdev, &pim->ImageMatrix, 1.0);
  369.     /*
  370.      * Check whether we've already made an XObject resource for this
  371.      * image.
  372.      */
  373.     if (id != gx_no_bitmap_id) {
  374.         piw->pres = pdf_find_resource_by_gs_id(pdev, resourceXObject, id);
  375.         if (piw->pres)
  376.         return 0;
  377.     }
  378.     }
  379.     /*
  380.      * We have to be able to control whether to put Pattern images in line,
  381.      * to avoid trying to create an XObject resource while we're in the
  382.      * middle of writing a Pattern resource.
  383.      */
  384.     if (for_pattern < 0)
  385.     pputs(pdev->strm, "q ");
  386.     if ((code = pdf_begin_write_image(pdev, piw, id, w, h, NULL, in_line)) < 0 ||
  387.     (code = pdf_color_space(pdev, &cs_value, &cs,
  388.                 &piw->pin->color_spaces, in_line)) < 0 ||
  389.     (code = psdf_setup_lossless_filters((gx_device_psdf *) pdev,
  390.                         &piw->binary,
  391.                         (gs_pixel_image_t *)pim)) < 0 ||
  392.     (code = pdf_begin_image_data(pdev, piw, (const gs_pixel_image_t *)pim,
  393.                      &cs_value)) < 0
  394.     )
  395.     return code;
  396.     pos = stell(pdev->streams.strm);
  397.     pdf_copy_color_bits(piw->binary.strm, row_base, sourcex, row_step, w, h,
  398.             bytes_per_pixel);
  399.     cos_stream_add_since(piw->data, pos);
  400.     pdf_end_image_binary(pdev, piw, piw->height);
  401.     return pdf_end_write_image(pdev, piw);
  402. }
  403.  
  404. int
  405. gdev_pdf_copy_color(gx_device * dev, const byte * base, int sourcex,
  406.             int raster, gx_bitmap_id id, int x, int y, int w, int h)
  407. {
  408.     gx_device_pdf *pdev = (gx_device_pdf *) dev;
  409.     gs_image_t image;
  410.     pdf_image_writer writer;
  411.     int code;
  412.     
  413.     if (w <= 0 || h <= 0)
  414.     return 0;
  415.     code = pdf_open_page(pdev, PDF_IN_STREAM);
  416.     if (code < 0)
  417.     return code;
  418.     /* Make sure we aren't being clipped. */
  419.     pdf_put_clip_path(pdev, NULL);
  420.     code = pdf_copy_color_data(pdev, base, sourcex, raster, id, x, y, w, h,
  421.                    &image, &writer, 0);
  422.     switch (code) {
  423.     default:
  424.         return code;    /* error */
  425.     case 1:
  426.         return 0;
  427.     case 0:
  428.         return pdf_do_image(pdev, writer.pres, NULL, true);
  429.     }
  430. }
  431.  
  432. /* Fill a mask. */
  433. int
  434. gdev_pdf_fill_mask(gx_device * dev,
  435.          const byte * data, int data_x, int raster, gx_bitmap_id id,
  436.            int x, int y, int width, int height,
  437.            const gx_drawing_color * pdcolor, int depth,
  438.            gs_logical_operation_t lop, const gx_clip_path * pcpath)
  439. {
  440.     gx_device_pdf *pdev = (gx_device_pdf *) dev;
  441.  
  442.     if (width <= 0 || height <= 0)
  443.     return 0;
  444.     if (depth > 1 || !gx_dc_is_pure(pdcolor) != 0)
  445.     return gx_default_fill_mask(dev, data, data_x, raster, id,
  446.                     x, y, width, height, pdcolor, depth, lop,
  447.                     pcpath);
  448.     return pdf_copy_mono(pdev, data, data_x, raster, id, x, y, width, height,
  449.              gx_no_color_index, gx_dc_pure_color(pdcolor),
  450.              pcpath);
  451. }
  452.  
  453. /* Tile with a bitmap.  This is important for pattern fills. */
  454. int
  455. gdev_pdf_strip_tile_rectangle(gx_device * dev, const gx_strip_bitmap * tiles,
  456.                   int x, int y, int w, int h,
  457.                   gx_color_index color0, gx_color_index color1,
  458.                   int px, int py)
  459. {
  460.     gx_device_pdf *const pdev = (gx_device_pdf *) dev;
  461.     int tw = tiles->rep_width, th = tiles->rep_height;
  462.     double xscale = pdev->HWResolution[0] / 72.0,
  463.     yscale = pdev->HWResolution[1] / 72.0;
  464.     bool mask;
  465.     int depth;
  466.     int (*copy_data)(P12(gx_device_pdf *, const byte *, int, int,
  467.              gx_bitmap_id, int, int, int, int,
  468.              gs_image_t *, pdf_image_writer *, int));
  469.     pdf_resource_t *pres;
  470.     cos_value_t cs_value;
  471.     int code;
  472.  
  473.     if (tiles->id == gx_no_bitmap_id || tiles->shift != 0 ||
  474.     (w < tw && h < th) ||
  475.     color0 != gx_no_color_index ||
  476.     /* Pattern fills are only available starting in PDF 1.2. */
  477.     pdev->CompatibilityLevel < 1.2
  478.     )
  479.     goto use_default;
  480.     if (color1 != gx_no_color_index) {
  481.     /* This is a mask pattern. */
  482.     mask = true;
  483.     depth = 1;
  484.     copy_data = pdf_copy_mask_data;
  485.     code = pdf_cs_Pattern_uncolored(pdev, &cs_value);
  486.     } else {
  487.     /* This is a colored pattern. */
  488.     mask = false;
  489.     depth = pdev->color_info.depth;
  490.     copy_data = pdf_copy_color_data;
  491.     code = pdf_cs_Pattern_colored(pdev, &cs_value);
  492.     }
  493.     if (code < 0)
  494.     goto use_default;
  495.     pres = pdf_find_resource_by_gs_id(pdev, resourcePattern, tiles->id);
  496.     if (!pres) {
  497.     /* Create the Pattern resource. */
  498.     int code;
  499.     long image_id, length_id, start, end;
  500.     stream *s;
  501.     gs_image_t image;
  502.     pdf_image_writer writer;
  503.     long image_bytes = ((long)tw * depth + 7) / 8 * th;
  504.     bool in_line = image_bytes <= MAX_INLINE_IMAGE_BYTES;
  505.     ulong tile_id =
  506.         (tw == tiles->size.x && th == tiles->size.y ? tiles->id :
  507.          gx_no_bitmap_id);
  508.  
  509.     if (in_line)
  510.         image_id = 0;
  511.     else if (image_bytes > 65500) {
  512.         /*
  513.          * Acrobat Reader can't handle image Patterns with more than
  514.          * 64K of data.  :-(
  515.          */
  516.         goto use_default;
  517.     } else {
  518.         /* Write out the image as an XObject resource now. */
  519.         code = copy_data(pdev, tiles->data, 0, tiles->raster,
  520.                  tile_id, 0, 0, tw, th, &image, &writer, 1);
  521.         if (code < 0)
  522.         goto use_default;
  523.         image_id = pdf_resource_id(writer.pres);
  524.     }
  525.     code = pdf_begin_resource(pdev, resourcePattern, tiles->id, &pres);
  526.     if (code < 0)
  527.         goto use_default;
  528.     s = pdev->strm;
  529.     pprintd1(s, "/Type/Pattern/PatternType 1/PaintType %d/TilingType 1/Resources<<\n",
  530.          (mask ? 2 : 1));
  531.     if (image_id)
  532.         pprintld2(s, "/XObject<</R%ld %ld 0 R>>", image_id, image_id);
  533.     pprints1(s, "/ProcSet[/PDF/Image%s]>>\n", (mask ? "B" : "C"));
  534.     /*
  535.      * Because of bugs in Acrobat Reader's Print function, we can't use
  536.      * the natural BBox and Step here: they have to be 1.
  537.      */
  538.     pprintg2(s, "/Matrix[%g 0 0 %g 0 0]", tw / xscale, th / yscale);
  539.     pputs(s, "/BBox[0 0 1 1]/XStep 1/YStep 1/Length ");
  540.     if (image_id) {
  541.         char buf[MAX_REF_CHARS + 6 + 1]; /* +6 for /R# Do\n */
  542.  
  543.         sprintf(buf, "/R%ld Do\n", image_id);
  544.         pprintd1(s, "%d>>stream\n", strlen(buf));
  545.         pprints1(s, "%sendstream\n", buf);
  546.         pdf_end_resource(pdev);
  547.     } else {
  548.         length_id = pdf_obj_ref(pdev);
  549.         pprintld1(s, "%ld 0 R>>stream\n", length_id);
  550.         start = pdf_stell(pdev);
  551.         code = copy_data(pdev, tiles->data, 0, tiles->raster,
  552.                  tile_id, 0, 0, tw, th, &image, &writer, -1);
  553.         switch (code) {
  554.         default:
  555.         return code;    /* error */
  556.         case 1:
  557.         break;
  558.         case 0:            /* not possible */
  559.         return_error(gs_error_Fatal);
  560.         }
  561.         end = pdf_stell(pdev);
  562.         pputs(s, "\nendstream\n");
  563.         pdf_end_resource(pdev);
  564.         pdf_open_separate(pdev, length_id);
  565.         pprintld1(pdev->strm, "%ld\n", end - start);
  566.         pdf_end_separate(pdev);
  567.     }
  568.     pres->object->written = true; /* don't write at end of page */
  569.     }
  570.     /* Fill the rectangle with the Pattern. */
  571.     {
  572.     int code = pdf_open_page(pdev, PDF_IN_STREAM);
  573.     stream *s;
  574.  
  575.     if (code < 0)
  576.         goto use_default;
  577.     /* Make sure we aren't being clipped. */
  578.     pdf_put_clip_path(pdev, NULL);
  579.     s = pdev->strm;
  580.     /*
  581.      * Because of bugs in Acrobat Reader's Print function, we can't
  582.      * leave the CTM alone here: we have to reset it to the default.
  583.      */
  584.     pprintg2(s, "q %g 0 0 %g 0 0 cm\n", xscale, yscale);
  585.     cos_value_write(&cs_value, pdev);
  586.     pputs(s, " cs");
  587.     if (mask)
  588.         pprintd3(s, " %d %d %d", (int)(color1 >> 16),
  589.              (int)((color1 >> 8) & 0xff), (int)(color1 & 0xff));
  590.     pprintld1(s, "/R%ld scn", pdf_resource_id(pres));
  591.     pprintg4(s, " %g %g %g %g re f Q\n",
  592.          x / xscale, y / yscale, w / xscale, h / xscale);
  593.     }
  594.     return 0;
  595. use_default:
  596.     return gx_default_strip_tile_rectangle(dev, tiles, x, y, w, h,
  597.                        color0, color1, px, py);
  598. }
  599.